<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="open" :width="dialogWidth" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="false" v-show="showSearch" label-width="85px">
        <el-row>
          <el-col :span="5">
            <el-form-item label="客户名称" prop="customer">
              <el-input
                v-model="queryParams.customer"
                placeholder="请输入客户名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>

          <el-col :span="5">
            <el-form-item label="发票状态" prop="state">
              <el-select v-model="queryParams.state" placeholder="请选择发票状态" clearable size="small">
                <el-option
                  v-for="dict in stateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="开票日期" prop="invoiceDate">
              <el-date-picker clearable size="small"
                              v-model="queryParams.invoiceDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择收款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="开票人" prop="invoiceUser">
              <el-input
                v-model="queryParams.invoiceUser"
                placeholder="请输入开票人"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange" highlight-current-row>
        <el-table-column label="发票编号" align="center" prop="code" />
        <el-table-column label="客户名称" align="center" prop="customer" />
        <el-table-column label="合同" align="center" prop="contractId" />
        <el-table-column label="发票状态" align="center" prop="state" :formatter="stateFormat" />
        <el-table-column label="税率" align="center" prop="taxRate" />
        <el-table-column label="税额" align="center" prop="taxAmount" />
        <el-table-column label="发票金额" align="center" prop="invoiceAmount" />
        <el-table-column label="开票日期" align="center" prop="invoiceDate" />
        <el-table-column label="开票人" align="center" prop="invoiceUser" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini" type="text"
              icon="el-icon-copy-document"
              @click="selectItem(scope.row)"
              v-hasPermi="['contract:main:edit']"
            >带回</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listInvoice, getInvoice } from "@/api/contract/invoice";

export default {
  name: "Invoice",
  components: {
  },props:{
    invoiceOpen: false,
    dialogWidth: {
      type:String,
      default:'1850px'
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票管理表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //合同选择框显示
      contractOpen:false,
      // 发票状态字典
      stateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        customer: null,
        contractId: null,
        state: null,
        taxRate: null,
        taxAmount: null,
        invoiceAmount: null,
        invoiceDate: null,
        invoiceUser: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.queryParams.contractId = this.$route.query.contractId;
    this.getList();
    this.getDicts("invoice_state").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询发票管理列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getRowByGid(gid){
      getInvoice(gid).then(response => {
        this.$emit('selectInvoice',response.data);
      });
    },
    //选择带回
    selectItem(row){
      this.$emit('selectInvoice',row);
    },
    // 发票状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //监测路由变化，刷新页面
    watchRoute(){
      this.queryParams.contractId = this.$route.query.contractId;
      this.getList();
      },
    // 表单重置
    reset() {
      this.form = {
        gid: null,
        code: null,
        customer: null,
        contractId: null,
        state: null,
        taxRate: null,
        taxAmount: null,
        invoiceAmount: null,
        invoiceDate: null,
        invoiceUser: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    }
  },
  watch:{
    "invoiceOpen"(val){
      this.open=val;
    },
    "open"(val){
      this.$emit('showInvoiceStateSync',val);
    }
  }
};
</script>

